<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Services\MyPimpableTrait;

class Section extends Model
{
    use HasFactory, MyPimpableTrait;

    const SECTION_LEVEL = 8;

    public $searchable = ['status', 'id', 'name', 'pid'];

    public $sortable = ['created_at'];

    public $defaultSortCriteria = ['created_at,desc'];

    protected $fillable = ['name', 'pid', 'status', 'level', 'path'];

    protected $vague = ['name'];


    // 和用户的关联
    public function users()
    {
        return $this->belongsToMany(User::class, 'user_has_section', 'section_id', 'user_id')
            ->withPivot('id')->using(UserHasSection::class);
    }


    // 和自己子级的关联
    public function children()
    {
        return $this->hasMany(Section::class, 'pid', 'id');
    }

    public function parent()
    {
        return $this->hasMany(Section::class, 'id', 'pid');
    }
}
